<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?>
<!--返回错误信息-->
<?php echo validation_errors(); ?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php
    $title = [
        'title' => '库存管理-商品入库',
    ];
    $this->load->view('/admin/common/header', $title);
    ?>
</head>

<body data-type="widgets">
<script src="/assets/js/theme.js"></script>
<div class="am-g tpl-g">

    <!--    头部   侧边菜单-->
    <?php $this->load->view('/admin/common/bodyLeft'); ?>
    <script type="text/javascript">
    	$(".js-sidebar-nav>li:nth-child(10)>ul").show(80);
    	$(".js-sidebar-nav>li:nth-child(10)>a>span").addClass("sidebar-nav-sub-ico-rotate");
    	$(".js-sidebar-nav>li:nth-child(10)>ul>li:nth-child(3)>a").addClass("active");
    </script>
    <!-- 内容区域 -->
    <div class="tpl-content-wrapper  stock-put">
        <div class="row-content am-cf">
            <div class="row">
                <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
                    <div class="widget am-cf">
                        <div class="widget-head am-cf">
                            <ol class="am-breadcrumb am-breadcrumb-slash">
                                <li><a href="/admin/stock/index?page=1">库存管理</a></li>
                                <li class="am-active">商品入库</li>
                            </ol>
                        </div>
                        <div class="widget-body  am-fr">
                            <form action="/admin/warning/edit" id="form_admin" method="post">
                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-6 am-form-group">
                                    <div class="am-btn-toolbar">
                                        <div class="am-alert am-alert-danger" id="data-alert" style="display: none;margin: 4px 0 10px 10px;font-size: 1.6rem;">
                                            <p>开始日期应小于结束日期</p>
                                        </div>
                                        <div class="am-g">
                                            <div class="am-u-sm-6">
                                                <button type="button" class="am-btn am-btn-default am-margin-right" id="my-start">开始日期</button><span id="my-startDate"></span>
                                            </div>
                                            <div class="am-u-sm-6">
                                                <button type="button" class="am-btn am-btn-default am-margin-right" id="my-end">结束日期</button><span id="my-endDate"></span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">

                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                    <div class="am-form-group tpl-table-list-select">
                                        <select data-am-selected="{btnSize: 'sm',btnWidth: '100%',maxHeight: 200}" id="supplier">
                                            <option value="null">请选择供应商</option>
                                            <?php
                                            foreach ($supplier as $k => $v) {
                                                echo '<option value="'.$v['name'].'" >'.$v['name'].'</option>';
                                            }
                                            ?>
                                        </select>
                                    </div>
                                </div>
                                <div class="am-u-sm-12 am-u-md-6 am-u-lg-6">
                                    <div class="am-form-group am-input-group am-input-group-sm tpl-form-border-form cl-p">
    <!--                                    <form action="/admin/user/search" method="get">-->
                                        <input type="text" class="am-form-field" name="search" id="searchValue">
                                        <span class="am-input-group-btn">
                                            <a href="javascript:void(0);" id="search" class="am-btn am-btn-default am-btn-success tpl-table-list-field am-icon-search">搜索</a>
                                        </span>
    <!--                                    </form>-->
                                    </div>
                                </div>
                            </div>
                                <div class="am-u-sm-12">
                                    <div class="am-btn-toolbar am-u-sm-12">
                                        <div class=" am-btn-group-xs" id="status">
                                            <a href="javascript:void(0);" class="am-btn ">待入库单</a>
                                            <a href="javascript:void(0);" class="am-btn ">已入库单</a>
                                            <a href="/admin/stock/putAddView" class="am-btn  am-btn-default">新增入库单</a>
                                        </div>
                                    </div>
                                </div>
                                <div class="am-u-sm-12">
                                    <table width="100%" class="am-table am-table-compact am-table-striped tpl-table-black "
                                           id="example-r">
                                        <thead>
                                        <tr>
                                            <th>序号</th>
                                            <th>单据日期</th>
                                            <th>单据编号</th>
                                            <th>关联单据名称</th>
                                            <th>业务类型</th>
                                            <th>供应商</th>
                                            <th>种类/数量</th>
                                            <th>状态</th>
                                            <th>操作</th>
                                        </tr>
                                        </thead>
                                        <tbody class="choose-box">
                                        <?php
                                        foreach ($list as $v) {
//                                            $v['status'] = $v['status'] ? $v['status']='已收货': $v['status'] = '未收货';
                                            echo '<tr class="gradeX">';

                                            echo   '<td>'.$v['id'].'</td>
                                                 <td>'.date('Y-m-d',$v['createTime']).'</td>
                                                 <td><a href="/admin/stock/putDetail?id='.$v['id'].'">'.$v['stockPutCode'].'</a></td>';
                                        $v['returnId'] = isset($v['returnId']) ? $v['returnId'] : '';
                                        $v['returnName'] = isset($v['returnName']) ? $v['returnName'] : '';

                                            if(isset($v['returnName']) && $v['returnName']){
                                                echo '<td><a href="/admin/procurement/purchaseEditView?id='.$v['returnId'].'&status='.$v['returnStatus'].'">'.$v['returnName'].'</a></td>';
                                            }else{
                                                echo '<td></td>';

                                            }


                                         echo '      <td>'.$v['type'].'</td>
                                               <td>'.$v['supplier'].'</td>
                                               <td>'.$v['attribute'].'</td>';

                                            switch ($v['status']){
                                                case 0:
                                                    echo '<td>未收货</td><td>
                                                    <div class="tpl-table-black-operation">
                                                     <a href="/admin/stock/putDetail?id='.$v['id'].'">
                                                        <i class="am-icon-truck"></i> 收货
                                                    </a>
                                                    </div>
                                                 </tr>';
                                                    break;
                                                case 1:
                                                    echo '<td>已收货</td><td>
                                                    <div class="tpl-table-black-operation">
                                                    <a href="/admin/stock/invalid?id='.$v['id'].'&type=put" class="tpl-table-black-operation-del">
                                                        <i class="am-icon-times-circle"></i> 作废
                                                    </a>
                                                    </div>
                                                 </tr>';
                                                    break;
                                                case 2:
                                                    echo '<td>待提交</td><td>
                                                    <div class="tpl-table-black-operation">
                                                    <a href="/admin/stock/putEditVIew?id='.$v['id'].'" class="tpl-table-black-operation-del">
                                                        <i class="am-icon-times-circle"></i> 编辑
                                                    </a>
                                                     <a href="/admin/stock/putRemove?id='.$v['id'].'" class="tpl-table-black-operation-del">
                                                        <i class="am-icon-times-circle"></i> 删除
                                                    </a>
                                                </div>
                                        </tr>';
                                                    break;
                                                case 3:
                                                    echo '<td>待审核</td><td>
                                                    <div class="tpl-table-black-operation">
                                                   
                                                </div>
                                        </tr>';
                                                    break;
                                                    
                                            }

                                        }
                                        ?>
                                        </tbody>
                                    </table>
                                </div>
                                <div class="am-u-lg-12">
                                    <div class="am-u-sm-centered">
                                        <ul class="am-pagination am-pagination-centered" id="paging">
                                            
                                        </ul>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</div>
<script>
    function GetQueryString(name)
    {
        var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if(r!=null)return  unescape(r[2]); return null;
    }
</script>
<script>
    $(".checkall").click(function() {
        var isChecked = $(this).prop("checked");

        $('.checkitem').each(function () {
              $(".checkitem").prop("checked", isChecked);
        });

    });
</script>
<script src="/assets/js/amazeui.min.js"></script>
<script src="/assets/js/amazeui.datatables.min.js"></script>
<script src="/assets/js/dataTables.responsive.min.js"></script>
<script src="/assets/js/app.js"></script>
<script src="/assets/js/page.js"></script>
<script>
    //筛选
    var startTime,
        endTime,
        supplier,
        status,
        searchValue,
        page;
    $("#status").on("click","a",function(){
        $(this).addClass("am-btn-primary");
        $(this).siblings().removeClass("am-btn-primary");

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        supplier = $("#supplier").val();
        status = getStatus();
        searchValue = $("#searchValue").val();
        page = 1;

        chooseWarning(startTime,endTime,supplier,status,searchValue,page);

    });

    $("#supplier").on( "change" , function() {
        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        supplier = $("#supplier").val();
        status = getStatus();
        searchValue = $("#searchValue").val();
        page = 1;

        chooseWarning(startTime,endTime,supplier,status,searchValue,page);

    });
    $("#search").on( "click" , function() {
        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        supplier = $("#supplier").val();
        status = getStatus();
        searchValue = $("#searchValue").val();
        page = 1;

        chooseWarning(startTime,endTime,supplier,status,searchValue,page);
    });

    $("#category").on( "change" , function() {
        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        supplier = $("#supplier").val();
        status = getStatus();
        searchValue = $("#searchValue").val();
        page = 1;

        chooseWarning(startTime,endTime,supplier,status,searchValue,page);
    });

    //筛选日期
    var startDate = new Date(2014, 11, 20);
    var endDate = new Date(2100, 11, 25);
    var $alert = $('#data-alert');
    $('#my-start').datepicker().on('changeDate.datepicker.amui', function(event) {
        
        if (event.date.valueOf() > endDate.valueOf()) {
          $alert.find('p').text('开始日期应小于结束日期！').end().show();
          return false;
          
        } else {
          $alert.hide();
          startDate = new Date(event.date);
          $('#my-startDate').text($('#my-start').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        supplier = $("#supplier").val();
        status = getStatus();
        searchValue = $("#searchValue").val();
        page = 1;

        chooseWarning(startTime,endTime,supplier,status,searchValue,page);
        
    });
    $('#my-end').datepicker().on('changeDate.datepicker.amui', function(event) {
        
        if (event.date.valueOf() < startDate.valueOf()) {
          $alert.find('p').text('结束日期应大于开始日期！').end().show();
          return false;
        } else {
          $alert.hide();
          endDate = new Date(event.date);
          $('#my-endDate').text($('#my-end').data('date'));
        }
        $(this).datepicker('close');

        startTime = $("#my-startDate").text();
        endTime = $("#my-endDate").text();
        supplier = $("#supplier").val();
        status = getStatus();
        searchValue = $("#searchValue").val();
        page = 1;

        chooseWarning(startTime,endTime,supplier,status,searchValue,page);
       
    });

    function getStatus() {
        var status;
        var statusA = $("#status").children("a.am-btn-primary");
        if(statusA.text() == "待入库单"){
            return status = 0;
        }else if(statusA.text() == "已入库单"){
            return status = 1;
        }else{
            return status = null;
        }
    }

    function chooseWarning(startTime,endTime,supplier,status,searchValue,page){
        $.ajax({
            type: "GET",
            url : "/api/stock/putSearch",
            data: {
                startTime:startTime,
                endTime: endTime,
                supplier: supplier,
                status: status,
                search: searchValue,
                page: page
            },
            dataType: "json",
            beforeSend:function(){
                $(".my-dark").css("display","block");
            },
            success: function(msg){
                var data = msg['0'];
                var list, 
                    listStatus,
                    operation;       //操作
                console.log(data);
                if(!data[0]){
                    list += '<tr class="gradeX"><td colspan="8">暂无数据</td></tr>';
                }else{
                    for(var i = 0 ; i < data.length;i++ ){
                        
                        listStatus = data[i].status == 0 ? '未收货' :
                            data[i].status == 1 ? '已收货' :
                                data[i].status == 2 ? '待提交' :
                                    data[i].status == 3 ? '待审核' : '6666';

                        if(data[i].status == 0){
                            operation = '<a href="/admin/stock/putDetail?id='+ data[i].id +'"><i class="am-icon-truck"></i> 收货</a>';
                        }else if(data[i].status == 1){
                            operation = '<a href="/admin/stock/invalid?id='+ data[i].id +'&type=put" class="tpl-table-black-operation-del"><i class="am-icon-times-circle"></i> 作废</a>';

                        }else if(data[i].status == 2){
                            operation = '<a href="/admin/stock/putEditVIew?id='+ data[i].id +'" class="tpl-table-black-operation-del"><i class="am-icon-times-circle"></i> 编辑</a>'+
                                '<a href="/admin/stock/putRemove?id='+ data[i].id +'" class="tpl-table-black-operation-del"><i class="am-icon-times-circle"></i> 删除 </a>';

                        }else if(data[i].status == 3){
                            operation = '';

                        }
                        
                        list += '<tr class="gradeX">'
                                + '<td>'+ data[i].id +'</td>'
                                + '<td>'+ data[i].createTime +'</td>'
                                + '<td><a href="/admin/stock/putDetail?id='+ data[i].id +'">'+ data[i].stockPutCode +'</a></td>'
                                + '<td><a href="/admin/procurement/purchaseEditView?id='+ data[i].returnId +'&status='+  data[i].returnStatus +'">'+ data[i].returnName +'</a></td>'
                                + '<td>'+ data[i].type +'</td>'
                                + '<td>'+ data[i].supplier +'</td>'
                                + '<td>'+ listStatus +'</td>'
                                + '<td>'+ data[i].attribute +'</td>'
                                + '<td>'
                                    + '<div class="tpl-table-black-operation">'
                                        + operation 
                                    + '</div>'
                                +'</td>'
                             + '</tr>';
                    }

                }
                $(".choose-box").html(list);

                //分页
                //设置分页信息  
                var pageOptions = {  
                    AllowPaging: true,  
                    PageIndex: msg.this_page,      //设置当前页码  
                    PageSize: msg.pageNum,       //设置分页大小  
                    RecordCount: msg.total_num,  //设置数据总数  
                    // TotalPage: ,      //设置总页数
                    showPageCount: 4,  
                    onPageClick: function(pageIndex) {
                        chooseWarning(startTime,endTime,supplier,status,searchValue,pageIndex);
                    }  
                }  
                //初始化分页栏  
                $('#paging').pageBar(pageOptions); 

            },
            complete:function(){
                $(".my-dark").css("display","none");
            },
            error: function (data) {  
               //  alert(XMLHttpRequest.status);
               //  alert(XMLHttpRequest.readyState);
               //  alert(textStatus);
            }
        });
    }
</script>
<script>
    $(document).ready(function() {  
        //设置分页信息  
        var pageOptions = {  
            AllowPaging: true,  
            PageIndex: <?php echo $_GET['page'] ?>,      //设置当前页码  
            PageSize: <?php echo $pageNum ?>,       //设置分页大小  
            RecordCount: <?php echo $totalNum ?>,  //设置数据总数  
            // TotalPage: ,      //设置总页数
            showPageCount: 4,  
            onPageClick: function(pageIndex) {  
                var search = '<?php echo isset($_GET['search'])  ? $_GET['search'] : 0 ?>';
                var page = parseInt(pageIndex);
                if(search){
                    window.location.href = "?page="+ page + "&search=" + search; 
                }else {
                    window.location.href = "?page="+ page;
                }
                
            }  
        }  
        //初始化分页栏
        $('#paging').pageBar(pageOptions);      
    })  
</script>
</body>

</html>